//
//  AmuneViewController.swift
//  OnemeetingDemo
//
//  Created by 张衡 on 2020/5/25.
//  Copyright © 2020 www.onemeeting.1meetingIos. All rights reserved.
//

import Foundation

@objc protocol  AmuneDelegate {
    @objc func amune_action(viewController:AmuneViewController, senber:KIcon?)
}

class AmuneViewController: BaseViewController {
    
    var isLiving:Bool = true
    var delegate:AmuneDelegate?
    var dataKIcons:[KIcon] = []
    
    
    var dataL:[KIconGroup] = [] {
        didSet {
            self.initContents(inView: self.contentV)
        }
    }
    
    
    
    override func loadView() {
        let view = NSView.init(frame: NSRect.init(x: 0, y: 0, width: 320, height: 160))
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(237).cgColor
        self.view = view
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
    }
    
    func setupUI() {
        self.view.addSubview(contentV)
        contentV.snp.makeConstraints { (make) in
            make.height.equalTo(80)
            make.width.equalTo(self.dataL.count*120-20)
            make.center.equalToSuperview()
        }
        self.initContents(inView: contentV)
    }
    
    lazy var contentV: NSView = {
        let view = NSView()
        view.wantsLayer = true
        return view
    }()
    
    func initContents(inView: NSView) {
        
        for (_,icon) in self.dataKIcons.enumerated() {
            icon.removeFromSuperview()
        }
        
        for (inx,val) in self.dataL.enumerated() {
            let view = KIcon.init(data: val)
            view.wantsLayer = true
            view.layer?.cornerRadius = 5
            view.layer?.masksToBounds = true
            view.layer?.borderColor = color_gray(220).cgColor
            view.layer?.borderWidth = 1
            view.imageSize = CGSize.init(width: 40, height: 40)
            view.title.textColor = color_gray(80)
            view.tag = 100+inx
            view.tap = { tagView  in
                self.tapAction(senber: tagView)
            }
            inView.addSubview(view)
            self.dataKIcons.append(view)
            view.snp.makeConstraints { (make) in
                make.width.equalTo(100)
                make.height.equalTo(80)
                make.top.equalTo(0)
                make.left.equalTo(inx*(100+20))
            }
            
            
        }
    }
    
    func tapAction(senber:NSView) {
        let iconView = senber as? KIcon
        
        if let delegate = self.delegate  {
            delegate.amune_action(viewController: self, senber: iconView)
        }
       
    }
}
